import Ability from '@ohos.application.Ability'
import dataShare from '@ohos.data.dataShare'
import rpc from "@ohos.rpc";
import dataSharePredicates from '@ohos.data.dataSharePredicates'
import distributedData from '@ohos.data.distributedData';
import deviceManager from '@ohos.distributedHardware.deviceManager';

let kvManager;
let kvdbStore;
let devManager;


const BUNDLE_NAME = 'com.samples.myapplication';
const STORE_ID = 'storeId';
var seConnect = {
    onConnect:function (elementName, proxy) {
        console.log("[ttt] [DataShareTest] <<Consumer>> seConnect onConnect called.");
        console.log("[ttt] [DataShareTest] <<Consumer>> seConnect onConnect elementName = " + elementName);
        console.log("[ttt] [DataShareTest] <<Consumer>> seConnect onConnect proxy = " + proxy);
         let data = rpc.MessageParcel.create();
         let reply = rpc.MessageParcel.create();
         let option = new rpc.MessageOption();
         data.writeInterfaceToken("connect-test");
         data.writeInt(111);

         console.log("[ttt] [DataShareTest] <<Consumer>> seConnect onConnect sendRequest.");
         proxy.sendRequest(1, data, reply, option)
             .then(function (result) {
                 if (result.errCode === 0) {
                     // let msg = reply.readString();
                     let msg = result.reply.readInt();
                     console.log("[ttt] [DataShareTest] <<Consumer>> seConnect reply msg: " + msg);
                 } else {
                     console.log("[ttt] [DataShareTest] <<Consumer>> seConnect sendRequest failed, errCode: " + result.errCode);
                 }
                 // callback没有返回值，默认返回undefined
             }).catch(function (e) {
             console.log("[ttt] [DataShareTest] <<Consumer>> seConnect sendRequest got exception: " + e);
         }).finally (async () => {
             console.log("[ttt] [DataShareTest] <<Consumer>> seConnect sendRequest ends, reclaim parcel");
             data.reclaim();
             reply.reclaim();
         })
    },
    onDisconnect:function (elementName) {
        console.log("[ttt] [DataShareTest] <<Consumer>> seConnect onDisconnect");
    },
    onFailed:function (code) {
        console.log("[ttt] [DataShareTest] <<Consumer>> seConnect onFailed");
    }
};
let dseProxy = 0;
let dseUri = ("datashare:///com.samples.datasharetest.DataShare");

var dseConnect = {
    onConnect:function (elementName, proxy) {
        console.log("[ttt] [DataShareClientTest] <<Consumer>> dseConnect onConnect called.");
        dseProxy = proxy;
    },
    onDisconnect:function (elementName) {
        console.log("[ttt] [DataShareClientTest] <<Consumer>> dseConnect onDisconnect, elementName:" + elementName);
    },
    onFailed:function (code) {
        console.log("[ttt] [DataShareClientTest] <<Consumer>> dseConnect onFailed, code:" + code);
    }
};

let seConnectionId = -1;
let dseConnectionId = -1;

let seWant = {
    "bundleName": "com.samples.datasharetest",
    "abilityName": "ServiceExtAbility",
}

let dseWant = {
    "bundleName": "com.samples.datasharetest",
    "abilityName": "DataShareExtAbility",
}

export function onCallback() {
    console.info("[ttt] [DataShareClientTest] <<Consumer>> **** Observer on callback ****");
}

export function offCallback() {
    console.info("[ttt] [DataShareClientTest] <<Consumer>> **** Observer off callback ****");
}

export default class MainAbility extends Ability {
    onCreate(want, launchParam) {
        // Ability is creating, initialize resources for this ability
        console.log("[ttt] [DataShareClientTest] <<Consumer>> MainAbility onCreate")
        globalThis.abilityWant = want;
    }

    onDestroy() {
        // Ability is destroying, release resources for this ability
        console.log("[ttt] [DataShareClientTest] <<Consumer>> MainAbility onDestroy")
    }

    onWindowStageCreate(windowStage) {
        // Main window is created, set main page for this ability
        console.log("[ttt] [DataShareClientTest] <<Consumer>> MainAbility onWindowStageCreate")
        globalThis.abilityContext = this.context;

        let context = this.context;
        dseConnectionId = context.connectAbility(dseWant, dseConnect);

        let deviceIds = [];
        deviceManager.createDeviceManager(BUNDLE_NAME, (err, value) => {
            if (err) {
                console.info("[ttt] [DataShareTest] <<Provider>> [createDeviceManager] err = " + err);
            }
            devManager = value;
            console.info('[ttt] [DataShareTest] <<Provider>> [createDeviceManager] enter');
            // get deviceIds
            if (devManager != null) {
                var devices = devManager.getTrustedDeviceListSync();
                for (var i = 0; i < devices.length; i++) {
                    deviceIds[i] = devices[i].deviceId;
                }
            }
            console.log("[ttt] [DataShareTest] <<Provider>> deviceIds :" + deviceIds);
        });

        globalThis.connectDataShareExtAbility = (async () => {
            console.log("[ttt] [DataShareTest] <<Consumer>> connectDataShareExtAbility begin");
            await dataShare.createDataShareHelper(globalThis.abilityContext, dseUri, (err,data)=>{
                globalThis.dsHelper = data;
                console.info("[ttt] [DataShareTest] <<Consumer>> ----- 1 -----, globalThis.dsHelper = " + globalThis.dsHelper);
                console.info("[ttt] [DataShareTest] <<Consumer>> ----- 2 -----, data = " + data);
                console.info("[ttt] [DataShareTest] <<Consumer>> ----- 3 -----, err = " + err);
                console.info("[ttt] [DataShareTest] <<Consumer>> ----- 4 -----, JSON.stringify(err) = " + JSON.stringify(err));
            });
            dseConnectionId = await context.connectAbility(dseWant, dseConnect);
        })

        globalThis.disconnectDataShareExtAbility = (async () => {
          console.log("[ttt] [DataShareClientTest] <<Consumer>> disconnectDataShareExtAbility begin");
          await context.disconnectAbility(dseConnectionId).then((data) => {
            console.info("[ttt] [DataShareClientTest] <<Consumer>> disconnectDataShareExtAbility success:" + JSON.stringify(data));
          }).catch((error) => {
            console.error("[ttt] [DataShareClientTest] <<Consumer>> disconnectDataShareExtAbility failed:" + JSON.stringify(error));
          });
          globalThis.dsHelper = null;
          console.log("[ttt] [DataShareClientTest] <<Consumer>> disconnectDataShareExtAbility end, dseConnectionId:" + dseConnectionId);
        })

        globalThis.on = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> on begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> on end, DSHelper is null");
                return;
            }
            await globalThis.dsHelper.on("dataChange", dseUri, onCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> on end");
        })

        globalThis.off = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> off begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> off end, DSHelper is null");
                return;
            }
            await globalThis.dsHelper.off("dataChange", dseUri);
            await globalThis.dsHelper.off("dataChange", dseUri, offCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> off end");
        })

        globalThis.query = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> query begin");
            let count = 0;
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> query end, DSHelper is null");
                return;
            }
            let getDa = await globalThis.getDa();
            let da = getDa.da;
            da.inDevices(deviceIds);
//            da.notEqualTo("$.name","wsx")
//            da.limit(2,2);
//            da.equalTo("$.name","rpf")
            let columnArray = getDa.columnArray;
            let result = await globalThis.dsHelper.query(dseUri, da, columnArray, globalThis.getResultSet);
            if (result != undefined) {
                count = result.rowCount;
            }
            console.log("[ttt] [DataShareClientTest] <<Consumer>> query end, count:" + count);
            return count;
        })

        globalThis.insert = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> insert begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> insert end, DSHelper is null");
                return;
            }
            let name: string = AppStorage.Get('insertName');
            let age: string = AppStorage.Get('insertAge');

            let booleanS: boolean = AppStorage.Get('trueFalseString');
            let vb = {"name":name, "age":Number(age), "isStudent": booleanS, "Binary": new Uint8Array([1,2,3])};
            console.log("[ttt] [DataShareClientTest] <<Consumer>> insert vb = " + JSON.stringify(vb));
            //            let vb = {"name":"rpf"};
            let ret = await globalThis.dsHelper.insert(dseUri, vb, globalThis.getInsertCallback);
            AppStorage.Delete('insertName');
            AppStorage.Delete('insertAge');
            AppStorage.Delete('trueFalseString');
            await globalThis.notifyChange();
            console.log("[ttt] [DataShareClientTest] <<Consumer>> insert end, ret:" + ret);
            return ret;
        })

        globalThis.update = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> update begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> update end, DSHelper is null");
                return;
            }
            let getDa = await globalThis.getDa();
            let da = getDa.da;
            let updateOption = getDa.updateOption;
            let ret = await globalThis.dsHelper.update(dseUri, da, updateOption, globalThis.getUpdateCallback);
            globalThis.notifyChange();
            console.log("[ttt] [DataShareClientTest] <<Consumer>> update end, result:" + ret);
        })

        globalThis.getUpdateCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getUpdateCallback enter");
            if(data == -1) {
                AppStorage.SetOrCreate('result', 'update操作失败');
            }else {
                AppStorage.SetOrCreate('result', 'update操作成功');
            }
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getUpdateCallback = " + data);
        })

        globalThis.delete = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> delete begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> delete end, DSHelper is null");
                return;
            }
            let getDa = await globalThis.getDa();
            let da = getDa.da;
            let ret = globalThis.dsHelper.delete(dseUri, da, globalThis.getDeleteCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> delete getDa Over");
            globalThis.notifyChange();
            console.log("[ttt] [DataShareClientTest] <<Consumer>> delete end, result:" + ret);
            return ret;
        })

        globalThis.batchInsert = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> batchInsert begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> batchInsert end, DSHelper is null");
                return;
            }
            let people = new Array(
                {"name": "LiSi", "age": 41, "isStudent": true, "Binary": new Uint8Array([1,2,3])},
                {"name": "WangWu", "age": 51, "isStudent": true, "Binary": new Uint8Array([1,2,3])},
                {"name": "ZhaoLiu", "age": 61, "isStudent": true, "Binary": new Uint8Array([1,2,3])});
            let ret = await globalThis.dsHelper.batchInsert(dseUri, people, globalThis.getBatchInsertCallback);
            globalThis.notifyChange();
            console.log("[ttt] [DataShareClientTest] <<Consumer>> batchInsert end, result:" + ret);
            return ret;
        })

        globalThis.getBatchInsertCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getBatchInsertCallback = " + data);
        })

        globalThis.batchInsertError = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> batchInsertError begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> batchInsertError end, DSHelper is null");
                return;
            }
            let people = new Array(
                {"name": "LiSi", "age": 41, "isStudent": true, "Binary": new Uint8Array([1,2,3])},
                {"name": "WangWu", "age": '51aaa', "isStudent": true, "Binary": new Uint8Array([1,2,3])},
                {"name": "ZhaoLiu", "age": 61, "isStudent": true, "Binary": new Uint8Array([1,2,3])});
            let ret = await globalThis.dsHelper.batchInsert(dseUri, people, globalThis.getBatchInsertCallback);
            globalThis.notifyChange();
            console.log("[ttt] [DataShareClientTest] <<Consumer>> batchInsertError end, result:" + ret);
            return ret;
        })
        globalThis.insertMaxLength = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> insertMaxLength begin");
//            let data = {
//                "key": dataMaxLength.key,
//                "value":dataMaxLength.value
//            }
//            console.info(`[ttt] [DataShareClientTest] <<Consumer>> dataMaxLength.keyLength = ${data.key.length}`);
//            console.info(`[ttt] [DataShareClientTest] <<Consumer>> dataMaxLength.valueLength = ${data.value.length}`);
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> insertMaxLength end, DSHelper is null");
                return;
            }
//            let ret = await globalThis.dsHelper.insert(dseUri, data, globalThis.getInsertCallback);
        })

        globalThis.insertKeyBeyondLimit = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> insertKeyBeyondLimit begin");
//            let data = {
//                "key": dataKeyBeyond.key,
//                "value":dataKeyBeyond.value
//            }
//            console.info(`[ttt] [DataShareClientTest] <<Consumer>> insertKeyBeyondLimit.keyLength = ${data.key.length}`);
//            console.info(`[ttt] [DataShareClientTest] <<Consumer>> insertKeyBeyondLimit.valueLength = ${data.value.length}`);
            if (globalThis.dsHelper == null) {
//                let ret = await globalThis.dsHelper.insert(dseUri, data, globalThis.getInsertCallback);
                console.log("[ttt] [DataShareClientTest] <<Consumer>> insertKeyBeyondLimit end, DSHelper is null");
                return;
            }
        })

        globalThis.insertValueBeyondLimit = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> insertValueBeyondLimit begin");
//            let data = {
//                "key": dataValueBeyond.key,
//                "value":dataValueBeyond.value
//            }
//            console.info(`[ttt] [DataShareClientTest] <<Consumer>> insertValueBeyondLimit.keyLength = ${data.key.length}`);
//            console.info(`[ttt] [DataShareClientTest] <<Consumer>> insertValueBeyondLimit.valueLength = ${data.value.length}`);
            if (globalThis.dsHelper == null) {
//                let ret = await globalThis.dsHelper.insert(dseUri, data, globalThis.getInsertCallback);
                console.log("[ttt] [DataShareClientTest] <<Consumer>> insertValueBeyondLimit end, DSHelper is null");
                return;
            }
        })

        globalThis.getResultSet = (async (err, resultSet) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getResultSet begin");
            globalThis.resultSet = resultSet;
            AppStorage.SetOrCreate('reaultSet', resultSet);
        })

        globalThis.getInsertCallback = (async (err, data) => {
            if(data == -1) {
                AppStorage.SetOrCreate('result', 'insert操作失败');
            }else {
                AppStorage.SetOrCreate('result', 'insert操作成功');
            }
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getInsertCallback = " + data);
        })

        globalThis.getDeleteCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getDeleteCallback enter ");
            if(data == -1) {
                AppStorage.SetOrCreate('result', 'delete操作失败');
            }else {
                AppStorage.SetOrCreate('result', 'delete操作成功');
            }
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getDeleteCallback = " + data);
        })

        globalThis.notifyChange = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> notifyChange begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> notifyChange end, DSHelper is null");
                return;
            }
            await globalThis.dsHelper.notifyChange(dseUri, globalThis.getNotifyChangeCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> notifyChange end");
        })

        globalThis.getNotifyChangeCallback = (async (err, data) => {
//            if(data == 1) {
//                AppStorage.SetOrCreate('result', '操作成功');
//            }else {
//                AppStorage.SetOrCreate('result', '操作失败');
//            }
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getNotifyChangeCallback = " + data);
        })


        globalThis.openFile = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> openFile begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> openFile end, DSHelper is null");
                return;
            }
            let result = await globalThis.dsHelper.openFile(dseUri,"rwt", globalThis.openFileCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> openFile end, result:" + result);
            return result;
        })

        globalThis.openFileCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getTypeCallback = " + data);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getTypeCallback json= " + JSON.stringify(data));
            AppStorage.SetOrCreate('result', JSON.stringify(data));
            //            if(err != -1) {
            //            }else {
            //                AppStorage.SetOrCreate('result', '操作失败');
            //            }
        })

        globalThis.getType = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getType begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> getType end, DSHelper is null");
                return;
            }
            let result = await globalThis.dsHelper.getType(dseUri, globalThis.getTypeCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getType end, result:" + result);
            return result;
        })

        globalThis.getTypeCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getTypeCallback = " + data);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getTypeCallback json= " + JSON.stringify(data));
            AppStorage.SetOrCreate('result', data);
//            if(err != -1) {
//            }else {
//                AppStorage.SetOrCreate('result', '操作失败');
//            }
        })

        globalThis.getFileTypes = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getFileTypes begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> getFileTypes end, DSHelper is null");
                return;
            }
            let result = await globalThis.dsHelper.getFileTypes(dseUri, "image/*", globalThis.getFlieTypesCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getFileTypes end, result:" + result);
            return result;
        })

        globalThis.getFlieTypesCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getFlieTypesCallback = " + data);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getFlieTypesCallback json= " + JSON.stringify(data));
            AppStorage.SetOrCreate('result', JSON.stringify(data));
            /*if(err != -1) {
            }else {
                AppStorage.SetOrCreate('result', '操作失败');
            }*/
        })

        globalThis.normalizeUri = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> normalizeUri begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> normalizeUri end, DSHelper is null");
                return;
            }
            let result = await globalThis.dsHelper.normalizeUri(dseUri, globalThis.getNormalizaUriCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> normalizeUri end, result:" + result);
            return result;
        })

        globalThis.getNormalizaUriCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getNormalizaUriCallback = " + data);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getNormalizaUriCallback json= " + JSON.stringify(data));
            AppStorage.SetOrCreate('result', data);
            /*if(err != -1) {
            }else {
                AppStorage.SetOrCreate('result', '操作失败');
            }*/
        })

        globalThis.denormalizeUri = (async () => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> denormalizeUri begin");
            if (globalThis.dsHelper == null) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> denormalizeUri end, DSHelper is null");
                return;
            }
            let result = await globalThis.dsHelper.denormalizeUri(dseUri, globalThis.getDenormalizaUriCallback);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> denormalizeUri end, result:" + result);
            return result;
        })

        globalThis.getDenormalizaUriCallback = (async (err, data) => {
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getDenormalizaUriCallback = " + data);
            console.log("[ttt] [DataShareClientTest] <<Consumer>> getDenormalizaUriCallback json= " + JSON.stringify(data));
            AppStorage.SetOrCreate('result', data);
            /*if(err != -1) {
            }else {
                AppStorage.SetOrCreate('result', '操作失败');
            }*/
        })

        globalThis.goToFirstRow = ( () => {
            let ret = globalThis.resultSet.goToFirstRow();
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> goToFirstRow')
        })

        globalThis.goToLastRow = ( () => {
            let ret = globalThis.resultSet.goToLastRow();
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> goToLastRow')
        })

        globalThis.goToNextRow = ( () => {
            let ret = globalThis.resultSet.goToNextRow();
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> goToNextRow')
        })

        globalThis.goToPreviousRow = ( () => {
            let ret = globalThis.resultSet.goToPreviousRow();
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> goToPreviousRow')
        })

        globalThis.goTo = ( (data) => {
            let ret = globalThis.resultSet.goTo(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> goTo')
        })

        globalThis.goToRow = ( (data) => {
            let ret = globalThis.resultSet.goToRow(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> goToRow')
        })

        globalThis.getBlob = ( (data) => {
            let ret = globalThis.resultSet.getBlob(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getBlob')
        })

        globalThis.getString = ( (data) => {
            let ret = globalThis.resultSet.getString(Number(data));
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getString' + ret);
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getString' + ret);
        })

        globalThis.getLong = ( (data) => {
            let ret = globalThis.resultSet.getLong(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getLong')
        })

        globalThis.getDouble = ( (data) => {
            let ret = globalThis.resultSet.getDouble(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getDouble')
        })

        globalThis.isColumnOrKeyNull = ( (data) => {
            let ret = globalThis.resultSet.isColumnOrKeyNull(data);
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> isColumnOrKeyNull')
        })

        globalThis.getColumnIndex = ( (data) => {
            let ret = globalThis.resultSet.getColumnOrKeyIndex(data);
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getColumnOrKeyIndex')
        })

        globalThis.getColumnName = ( (data) => {
            let ret = globalThis.resultSet.getColumnOrKeyName(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getColumnOrKeyName')
        })

        globalThis.getInt = ( (data) => {
            let ret = globalThis.resultSet.getInt(Number(data));
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getInt')
        })

        globalThis.getDataType = ( (data) => {
            let ret = globalThis.resultSet.getDataType(data);
            return ret;
            console.info('[ttt] [DataShareClientTest] <<Consumer>> getDataType')
        })

        globalThis.close = ( () => {
            globalThis.resultSet.close();
            console.info('[ttt] [DataShareClientTest] <<Consumer>> close')
        })

        globalThis.getDa = (async () => {
            let da = new  dataSharePredicates.DataSharePredicates();
            if (da == null || da == undefined) {
                console.log("[ttt] [DataShareClientTest] <<Consumer>> da is null or undefined");
                return;
            }
            let optionLength = AppStorage.Get('optionLength');
            console.info("[ttt] [DataShareClientTest] <<Consumer>> optionLength = " + optionLength)
            let updateOptionName = '';
            let updateOptionAge = '';
            let updateOptionBoolean = '';
//            let updateOption = {name: '', age: '', boolean: ''};
            let columnArray = [];
            for(let i = 0; i < optionLength; i++) {
                let predicate: {
                    "functionCheck": string,
                    "keyCheck": string,
                    "value": string
                };
                predicate = JSON.parse(AppStorage.Get('Predicates' + i));
               if(predicate.functionCheck == ''){
                    if (predicate.keyCheck != '' && predicate.value != '') {
                        if (predicate.keyCheck == 'name') {
                            updateOptionName = predicate.value;
                        } else if (predicate.keyCheck == 'age') {
                            updateOptionAge = predicate.value;
                        } else if (predicate.keyCheck == 'boolean') {
                            updateOptionBoolean = predicate.value;
                        }
                    } else {
                        columnArray.push(predicate.keyCheck);
                    }
                }
                else if(predicate.functionCheck == 'equalTo') {
                    da.equalTo(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'notEqualTo') {
                    da.notEqualTo(predicate.keyCheck, predicate.value);
                }else if (predicate.functionCheck == 'greaterThan') {
                    da.greaterThan(predicate.keyCheck, predicate.value);
                }else if (predicate.functionCheck == 'lessThan') {
                    da.lessThan(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'greaterThanOrEqualTo') {
                    da.greaterThanOrEqualTo(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'lessThanOrEqualTo') {
                   console.log("[ttt] [DataShareClientTest] <<Consumer>> lessThanOrEqualTo on");
                   da.lessThanOrEqualTo(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'isNull') {
                    da.isNull(predicate.keyCheck);
                }else if(predicate.functionCheck == 'isNotNull') {
                    da.isNotNull(predicate.keyCheck);
                }else if(predicate.functionCheck == 'in') {
                    da.in(predicate.keyCheck, predicate.value.split(','));
                }else if(predicate.functionCheck == 'notIn') {
                    da.notIn(predicate.keyCheck, predicate.value.split(','));
                }else if(predicate.functionCheck == 'like') {
                    da.like(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'unlike') {
                    da.unlike(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'and') {
                    da.and();
                }else if(predicate.functionCheck == 'or') {
                    da.or();
                }else if(predicate.functionCheck == 'orderByAsc') {
                    da.orderByAsc(predicate.keyCheck);
                }else if(predicate.functionCheck == 'orderByDesc') {
                    da.orderByDesc(predicate.keyCheck);
                }else if(predicate.functionCheck == 'limit') {
                   console.info("ttt Number(predicate.value.split(',')[0]) = " + Number(predicate.value.split(',')[0]));
                   console.info("ttt Number(predicate.value.split(',')[1]) = " + Number(predicate.value.split(',')[1]));
                    da.limit(Number(predicate.value.split(',')[0]),Number(predicate.value.split(',')[1]));
                }else if(predicate.functionCheck == 'isNotNull') {
                    da.isNotNull(predicate.keyCheck);
                }else if(predicate.functionCheck == 'beginWrap') {
                    da.beginWrap();
                }else if(predicate.functionCheck == 'endWrap') {
                    da.endWrap();
                }else if(predicate.functionCheck == 'prefixKey') {
                    da.prefixKey(predicate.value);
                }else if(predicate.functionCheck == 'contains') {
                    da.contains(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'beginsWith') {
                    da.beginsWith(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'endsWith') {
                    da.endsWith(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'glob') {
                    da.glob(predicate.keyCheck, predicate.value);
                }else if(predicate.functionCheck == 'between') {
                    da.between(predicate.keyCheck, predicate.value.split(',')[0], predicate.value.split(',')[1]);
                }else if(predicate.functionCheck == 'notBetween') {
                    da.notBetween(predicate.keyCheck, predicate.value.split(',')[0], predicate.value.split(',')[1]);
                }else if(predicate.functionCheck == 'distinct') {
                    da.distinct();
                }else if(predicate.functionCheck == 'groupBy') {
                    da.groupBy(predicate.value.split(','));
                }else if(predicate.functionCheck == 'indexedBy') {
                    da.indexedBy(predicate.keyCheck);
                }else if(predicate.functionCheck == 'inKeys') {
//                   let Array =
                   da.inKeys(predicate.value.split(','));
               }
            }
            let updateOption;
            if(updateOptionName != '') {
                updateOption  = {"name": updateOptionName};
            }else if(updateOptionAge != '') {
                updateOption  = { "age": Number(updateOptionAge)};
            }else if(updateOptionBoolean != '') {
                if(updateOptionBoolean == "true") {
                    updateOption  = {"isStudent": true};
                }else {
                    updateOption  = {"isStudent": false};
                }
            }
            let returnData = {updateOption : updateOption, da: da,columnArray: columnArray}
            returnData.updateOption = updateOption;
            returnData.da = da;
            if(columnArray.length == 0) {
                columnArray.push('*');
            }
            returnData.columnArray = columnArray;
            return returnData;
        })

        windowStage.setUIContent(this.context, "pages/index", null)
    }



    onWindowStageDestroy() {
        // Main window is destroyed, release UI related resources
        console.log("[ttt] [DataShareClientTest] <<Consumer>> MainAbility onWindowStageDestroy")
    }

    onForeground() {
        // Ability has brought to foreground
        console.log("[ttt] [DataShareClientTest] <<Consumer>> MainAbility onForeground")
    }

    onBackground() {
        // Ability has back to background
        console.log("[ttt] [DataShareClientTest] <<Consumer>> MainAbility onBackground")
    }
};
